<?php
declare (strict_types = 1);

namespace app\listener;

use app\admin\model\live\LiveGoods;
use app\admin\model\store\Goods;
use think\facade\Cache;

class LiveGoodsSales
{
    /**
     * 事件监听处理
     * @return mixed
     */
    public function handle($params)
    {

        //直播间id
        $liveId = $params['live_id'];
        //直播间商品id
        $goodsId = $params['goods_id'];
        //购买件数
        $num = $params['num'];
        $redis = Cache::store('redis');


        $redis->select(14);

        $where[] = ['live_id','=',$liveId];
        $where[] = ['goods_id','=',$goodsId];
        $bool = $redis->has("live:$liveId,goods:$goodsId");
        if ($bool){
            $redis->inc("live:$liveId,goods:$goodsId",$num);
            $sales = $redis->get("live:$liveId,goods:$goodsId");
            LiveGoods::where($where)->update(['sales_volume' => $sales]);
        }else{
            $redis->set("live:$liveId,goods:$goodsId",$num);
            LiveGoods::where($where)->update(['sales_volume' => $num]);
        }
    }
//        $redis = Cache::store('redis')->handler();
//        $redis->rpush();
//        $redis->lpop();
}
